<%@ page language="java" import="java.util.*,com.mongodb.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
         response.setContentType("text/html;charset=utf-8");  
         request.setCharacterEncoding("utf-8");           
         String username=(String)request.getParameter("username");  //获取用户名
         String pwd=(String)request.getParameter("pwd");//获取密码
         String rem=(String)request.getParameter("rem");//是否记住密码

         
	   	  try{   
	             // 连接到 mongodb 服务
	             MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
	             DB db = mongoClient.getDB( "blog" );  //连接到数据库blog
	             DBCollection coll = db.getCollection("user");  //获取blog数据库中集合user
	            
	             DBObject user = new BasicDBObject();//定义一个Bson变量，用于存储注册的用户名和密码
	             user.put("username", username);
                 user.put("pwd", pwd);
                 
	             DBCursor records = coll.find(user);
	             if(records.length()!=0){
	            	 
	            	 session.setAttribute("username",records.toArray().get(0).get("username"));
	            	 session.setAttribute("email",records.toArray().get(0).get("email"));
	                 session.setAttribute("pwd",records.toArray().get(0).get("pwd"));
	      			 
	            	 response.getWriter().write("true");
	 	       	     response.getWriter().close();
	             }else{
	            	 response.getWriter().write("账号密码错误，请重新输入！");
		             response.getWriter().close();
	             }
	          }catch(Exception e){
	       	     response.getWriter().write("false");
	       	     response.getWriter().close();
	             System.err.println( e.getClass().getName() + ": " + e.getMessage() );
	         }    
          
     %>
</body>
</html>